package cn.itcast.sms.action;

import cn.itcast.bos.utils.MailUtils;
import cn.itcast.crm.domain.Customer;
import cn.itcast.sms.utils.BaseAction;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.TimeUnit;


@Namespace("/")
@ParentPackage("json-default")
@Scope("protoType")
@Controller
public class RegistAction extends BaseAction<Customer> {
    //注入jmsTemplate,使用activeMq发送邮件
    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;
    //属性驱动
    //接受手机验证码
    private String checkCode;

    public void setCheckCode(String checkCode) {
        this.checkCode = checkCode;
    }
    //接受邮箱激活码
    private RedisTemplate<String,String> redisTemplate;

    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    //注册的方法
    @Action(value = "customer_regist",results = {@Result(name="success",type="redirect",location = "signup-success.html"),
                                                 @Result(name="input",type="redirect",location = "signup.html")})
    public String regist() throws Exception {
       String sessioncheckCode = (String ) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());
        if(!checkCode.equals(sessioncheckCode) ||sessioncheckCode ==null){
            ServletActionContext.getResponse().getWriter().print("短信验证码错误");
            return INPUT;
        }else{
            //调用webservice链接Crm
            WebClient.create("http://localhost:8081/crm_management/service/customerService/registCustomer").
                    type(MediaType.APPLICATION_JSON).post(model);
            //发送一封激活邮件
            final String activeCode = RandomStringUtils.randomNumeric(32);
            //将激活码存入redis中保存24小时
            redisTemplate.opsForValue().set(model.getTelephone(),activeCode,24, TimeUnit.HOURS);

            //调用MainUtils发送邮件
            final String content = "尊敬的用户,请于24之内完成邮箱绑定,请点击下面的链接:</br><a href='"+ MailUtils.activeUrl+"?telephone="+model.getTelephone()+"&activeCode="+activeCode+"'>速运快递邮箱绑定地址</a>";

            // MailUtils.sendMail("速运快递激活邮件",content,model.getEmail());
            jmsTemplate.send("bos_sendMail", new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    MapMessage mapMessage = session.createMapMessage();
                    mapMessage.setString("email",model.getEmail());
                    //mapMessage.setString("content",content);
                    mapMessage.setString("activeCode",activeCode);
                    mapMessage.setString("telephone",model.getTelephone());
                    return mapMessage;
                }
            });
            return SUCCESS;

        }

    }
}
